package com.zhrj.exam.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhrj.exam.dto.ExcelDto;
import com.zhrj.exam.entity.ZhrjAlarmInfo;
import com.zhrj.exam.mapper.ZhrjAlarmInfoMapper;
import com.zhrj.exam.service.IZhrjAlarmInfoService;
import com.zhrj.exam.vo.DeptStatisticVo;
import com.zhrj.exam.vo.StatisticVo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 事件表 服务实现类
 * </p>
 * @author zhrj
 * @since 2023-03-14
 */
@Service
public class ZhrjAlarmInfoServiceImpl extends ServiceImpl<ZhrjAlarmInfoMapper, ZhrjAlarmInfo> implements IZhrjAlarmInfoService {

    @Override
    public void importFile(MultipartFile file) throws IOException {
        // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
        // 具体需要返回多少行可以在`PageReadListener`的构造函数设置
        EasyExcel.read(file.getInputStream(), ExcelDto.class, new PageReadListener<ExcelDto>(dataList -> {
            List<ZhrjAlarmInfo> zhrjAlarmInfos = BeanUtil.copyToList(dataList, ZhrjAlarmInfo.class);
            saveBatch(zhrjAlarmInfos);
        })).sheet().doRead();
    }

    @Override
    public Map statistic() {
        // 查询不同的类型事件
        List<StatisticVo> statisticVos = new ArrayList<>();
        List<ZhrjAlarmInfo> all = list();
        Map<Integer, List<ZhrjAlarmInfo>> integerListMap =
                all.stream().collect(Collectors.groupingBy(ZhrjAlarmInfo::getEventType));
        for (Integer type : integerListMap.keySet()) {
            StatisticVo vo = new StatisticVo();
            vo.setEventType(type);
            vo.setCounts(integerListMap.get(type).size());
            statisticVos.add(vo);
        }

       // 查询不同的部门
        List<DeptStatisticVo> deptVos = new ArrayList<>();
        Map<Long, List<ZhrjAlarmInfo>> deptIdMap =
                all.stream().collect(Collectors.groupingBy(ZhrjAlarmInfo::getDeptId));
        for (Long deptId : deptIdMap.keySet()) {
            DeptStatisticVo vo = new DeptStatisticVo();
            vo.setDeptId(deptId);
            vo.setCounts(deptIdMap.get(deptId).size());
            deptVos.add(vo);
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("AlarmInfo", statisticVos);
        map.put("deptList", deptVos);
        return map;
    }

    @Override
    public List<Map> monthDateCount(Date date) {
        List<Map> vo = new ArrayList<>();

        List<ZhrjAlarmInfo> allList = list(new LambdaQueryWrapper<ZhrjAlarmInfo>()
                .ge(ZhrjAlarmInfo::getEventTime, DateUtil.beginOfMonth(date))
                .le(ZhrjAlarmInfo::getEventTime, DateUtil.endOfMonth(date))
        );
        Map<String, List<ZhrjAlarmInfo>> groupByDay = allList.stream().collect(Collectors.groupingBy(e -> {
            String eventTime = e.getEventTime();
            return DateUtil.format(DateUtil.parse(eventTime), "yyyy-MM-dd");
        }));
        for (String day : groupByDay.keySet()) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("eventDate", day);
            map.put("count", groupByDay.get(day).size());
            vo.add(map);
        }

        return vo;
    }

    @Override
    public List<ZhrjAlarmInfo> export(String type, Integer curr, Integer size) {
        List<ZhrjAlarmInfo> vo = new ArrayList<>();
        if (StrUtil.equals(type, "1")) {
            vo = page(new Page<>(curr, size), null).getRecords();
        } else if (StrUtil.equals(type, "2")) {
            vo = list();
        } else {
            throw new RuntimeException("type内容不正确");
        }

        return vo;
    }
}
